//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using DNAide.Drawing.Quantization;

namespace DNAide.Drawing
{
	public static partial class BitmapAide
	{

		/// <summary>
		/// Saves bitmap as a jpeg to a file
		/// </summary>
		/// <param name="bitmap"></param>
		/// <param name="filename"></param>
		/// <param name="quality"></param>
		public static void SaveAsJpegToFile(Bitmap bitmap, String filename, Int32 quality)
		{
			FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite);
			StreamWriter sw = new StreamWriter(fs);
			SaveAsJpegToStream(bitmap, sw.BaseStream, quality);
			sw.Close();
		}

		/// <summary>
		/// Saves bitmap as a gif to a file
		/// </summary>
		/// <remarks>
		/// Sets transparent color as Color.Transparent
		/// Uses Quantizer by default for better a better quality gif. 
		/// If you don't use the quantizer, the gif will be generated using windows default 256 palette
		/// </remarks>
		/// <param name="bitmap"></param>
		/// <param name="filename"></param>
		/// <param name="maxColors"></param>
		public static void SaveAsGifToFile(Bitmap bitmap, String filename, Int32 maxColors)
		{
			SaveAsGifToFile(bitmap, filename, maxColors, Color.Transparent);
		}

		/// <summary>
		/// Saves a bitmap as a gif to a file
		/// </summary>
		/// <param name="bitmap"></param>
		/// <param name="filename"></param>
		/// <param name="maxColors"></param>
		/// <param name="transparentColor"></param>
		public static void SaveAsGifToFile(Bitmap bitmap, String filename, Int32 maxColors, Color transparentColor)
		{
			FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite);
			StreamWriter sw = new StreamWriter(fs);
			SaveAsGifToStream(bitmap, sw.BaseStream, maxColors, transparentColor);
			sw.Close();
		}

		/// <summary>
		/// Saves bitmap as a jpeg to a stream
		/// </summary>
		/// <param name="bitmap"></param>
		/// <param name="outputStream"></param>
		/// <param name="quality"></param>
		public static void SaveAsJpegToStream(Bitmap bitmap, Stream outputStream, Int32 quality)
		{
			ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
			EncoderParameters encoderParameters;
			encoderParameters = new EncoderParameters(1);
			encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
			bitmap.Save(outputStream, info[1], encoderParameters);
		}

		/// <summary>
		/// Save bitmap as gif to stream
		/// </summary>
		/// <remarks>
		/// Sets transparent color as Color.Transparent
		/// Uses Quantizer by default for better a better quality gif. 
		/// If you don't use the quantizer, the gif will be generated using windows default 256 palette
		/// </remarks>
		/// <param name="bitmap"></param>
		/// <param name="outputStream"></param>
		/// <param name="maximumColors"></param>
		public static void SaveAsGifToStream(Bitmap bitmap, Stream outputStream, Int32 maximumColors)
		{
			SaveAsGifToStream(bitmap, outputStream, maximumColors, Color.Transparent);
		}

		/// <summary>
		/// Save bitmap as gif to stream
		/// </summary>
		/// <param name="bitmap"></param>
		/// <param name="outputStream"></param>
		/// <param name="maxColors"></param>
		/// <param name="transparentColor"></param>
		public static void SaveAsGifToStream(Bitmap bitmap, Stream outputStream, Int32 maxColors, Color transparentColor)
		{
			OctreeQuantizer quantizer = new OctreeQuantizer(maxColors, 8);

			using (Bitmap quantized = quantizer.Quantize(bitmap))
			{
				if (transparentColor != Color.Transparent)
				{
					quantized.Palette = SetTransparentColor(quantized.Palette, maxColors, transparentColor);
				}
				quantized.Save(outputStream, ImageFormat.Gif);
			}
		}

	}
}

